import time


class MeasureTime:
    def __init__(self, func):
        self.func = func
        # 可以添加计数器来记录调用次数和总时间
        self.call_count = 0
        self.total_time = 0.0

    def __call__(self, *args, **kwargs):
        # 记录开始时间
        start_time = time.perf_counter()

        # 执行被装饰的函数
        result = self.func(*args, **kwargs)

        # 计算执行时间
        end_time = time.perf_counter()
        execution_time = end_time - start_time

        # 更新统计信息
        self.call_count += 1
        self.total_time += execution_time

        # 打印执行时间信息
        print(f"{self.func.__name__} 执行时间: {execution_time:.6f}秒 "
              f"(累计调用{self.call_count}次，总耗时{self.total_time:.6f}秒)")

        return result


# 使用示例
@MeasureTime
def say_hello():
    print("hello")


@MeasureTime
def slow_function(seconds):
    print(f"等待{seconds}秒...")
    time.sleep(seconds)
    return "完成等待"


# 测试装饰器
say_hello()
say_hello()

slow_function(0.5)
slow_function(0.3)
